<?php

namespace App\Service\UserOperation;

use App\Constants\ErrorCode;
use App\Exception\ApiException;
use App\Model\PayOrder;
use App\Model\PlatformLimit;

class RechargeCoinCheckService
{
    /**
     * 充值的金额
     * @var
     */
    public $money;

    /**
     * 执行用户id
     * @var
     */
    public $actionUserId;

    public function check()
    {
        //获取限制 如果查不到限制数据就是没有限制
        $limit = PlatformLimit::where([
            ['type', '=', PlatformLimit::RECHARGE_POPOVER],
            ['status', '=', 1]
        ])->orderBy('id', 'desc')->value('value');
        if (empty($limit)) {
            return true;
        }

        $todayRechargeTotal = PayOrder::getSuccessTotal($this->actionUserId, date('Y-m-d'));

        $money = $this->money * 100;

        if ($money <= 0) {
            throw new ApiException('充值金额不可以小于0');
        }

        if ($money + $todayRechargeTotal >= $limit * 100) {
            //您今日充值频率过高，为保障您的资产安全，请警惕消谨防上当受骗。
            throw new ApiException('', ErrorCode::RECHARGE_POPOVER);
        }

        return true;
    }
}